package com.hzmall.controller;

import com.hzmall.common.ResponseCode;
import com.hzmall.common.ServerResponse;
import com.hzmall.pojo.User;
import com.hzmall.service.impl.CartService;
import com.hzmall.util.CheckLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/cart/")
public class CartController {
    @Autowired
    private CartService cartService;

    //显示购物车商品列表
    @RequestMapping(value = "list.do")
    @ResponseBody
    public ServerResponse list(HttpServletRequest request){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.cartService.list(currentUser.getId());
    }

    //购物车添加商品
    @RequestMapping(value = "add.do")
    @ResponseBody
    public ServerResponse add(HttpServletRequest request, Integer productId, Integer count){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.cartService.add(currentUser.getId(), productId,count);
    }

    //更新购物车某个产品数量
    @RequestMapping(value = "update.do")
    @ResponseBody
    public ServerResponse update(HttpServletRequest request, Integer productId, Integer count){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.cartService.update(currentUser.getId(), productId,count);
    }

    //移除购物车某个产品
    @RequestMapping(value = "delete_product.do")
    @ResponseBody
    public ServerResponse deleteProduct(HttpServletRequest request, Integer[] productIds){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.cartService.deleteProduct(currentUser.getId(),productIds);
    }

    //购物车选中某个商品
    @RequestMapping(value = "select.do")
    @ResponseBody
    public ServerResponse select(HttpServletRequest request,Integer productId){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.cartService.select(currentUser.getId(),productId);
    }

    //购物车取消选中某个商品
    @RequestMapping(value = "un_select.do")
    @ResponseBody
    public ServerResponse unSelect(HttpServletRequest request,Integer productId){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.cartService.unSelect(currentUser.getId(),productId);
    }

    //查询在购物车里的产品数量
    @RequestMapping(value = "get_cart_product_count.do")
    @ResponseBody
    public ServerResponse getCartProductCount(HttpServletRequest request){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.cartService.getCartProductCount(currentUser.getId());
    }

    //购物车全选
    @RequestMapping(value = "select_all.do")
    @ResponseBody
    public ServerResponse selectAll(HttpServletRequest request){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.cartService.selectAll(currentUser.getId());
    }

    //购物车全不选
    @RequestMapping(value = "un_select_all.do")
    @ResponseBody
    public ServerResponse unSelectAll(HttpServletRequest request){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.cartService.unSelectAll(currentUser.getId());
    }
}
